import net.spreadshirt.shop.lib.ProducttypeCategory;

/**
 * The ProducttypeCategories class stores all categoriy objects
 * for categorizing all available product types.
 */
class net.spreadshirt.shop.lib.ProducttypeCategories {

	// A list of all product type categories.
	private var _producttype_categories:Array;

	/**
	 * ProducttypeCategories Constructor
	 */
	public function ProducttypeCategories() {
		clear();
	}

	/**
	 * Clears all values.
	 */
	public function clear():Void {
		_producttype_categories = new Array();
	}

	/**
	 * Stores a list of product type categorie items.
	 * The old list will be overwritten.
	 * 
	 * @param	items	A list product type category items.
	 */
	public function addProducttypeCategories(items:Array):Void {
		// Clear articles list.
		clear();
		// Adds all items.
		for(var i:Number = 0; i < items.length; i++) {
			addProducttypeCategory(items[i]);
		}
	}

	/**
	 * Stores a single product type category in the categories list.
	 * 
	 * @param	item	A single article item.
	 */
	public function addProducttypeCategory(item:Object):Void {
		if(!isExisting(item)){
			var producttypeCategory:ProducttypeCategory
				= new ProducttypeCategory(item);
			_producttype_categories.push(producttypeCategory);
		}
	}

	/**
	 * Returns the number of stored product type categories.
	 * 
	 * @return	The number of items in the product type category list.
	 */
	public function getCount():Number {
		return _producttype_categories.length;
	}

	/**
	 * Returns the complete list of all product type category items.
	 * 
	 * @return	A list of product type category items.
	 */
	public function getAllItems():Array {
		return _producttype_categories;
	}

	/**
	 * Returns a single product type category item.
	 * 
	 * @param	id	The product type category ID.
	 * 
	 * @return	A product type category item.
	 */
	public function getItem(id:Number):ProducttypeCategory {
		var item:ProducttypeCategory;
		for(var i:Number = 0; i < _producttype_categories.length; i++) {
			if(id == _producttype_categories[i].producttype_category_id) {
				item = _producttype_categories[i];
			}
		}
		return item;
	}

	/**
	 * Checks if the product type category is already stored.
	 * 
	 * @param	item	A single category item.
	 * 
	 * @return	true or false
	 */
	private function isExisting(item:Object):Boolean {
		var found:Boolean = false;
		for(var i:Number = 0; i < _producttype_categories.length; i++) {
			if(item.producttype_category_id
				== _producttype_categories[i].producttype_category_id) {
				found = true;
				break;
			}
		}
		return found;
	}

}
